package com.wms.service.impl;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.ConfirmOutOrderDto;
import com.wms.dto.ItemSkuDto;
import com.wms.dto.OutOrderDto;
import com.wms.mapper.ItemSkuMapper;
import com.wms.mapper.OutboundOrderDetailsMapper;
import com.wms.mapper.OutboundOrderMapper;
import com.wms.pojo.*;
import com.wms.service.OutboundOrderService;
import com.wms.service.WarehouseAreaLocalService;
import com.wms.vo.OutBoundOrderVo;
import com.wms.vo.OutboundOutVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class OutboundOrderServiceImpl extends ServiceImpl<OutboundOrderMapper, OutboundOrder> implements OutboundOrderService {
    private final OutboundOrderDetailsMapper detailsMapper;
    private final ItemSkuMapper skuMapper;
    private final WarehouseAreaLocalService localService;

    @Override
    public PageResult queryPage(Integer page, Integer pageSize, Integer status, String invoiceCode,String customerName) {
        PageHelper.startPage(page,pageSize);
        List<OutBoundOrderVo> list = getBaseMapper().queryPage(status,invoiceCode,customerName);
        Page<OutBoundOrderVo> voPage = (Page<OutBoundOrderVo>) list;
        return new PageResult(voPage.getTotal(),voPage.getResult());
    }

    //添加
    @Override
    @Transactional
    public void saveOutBoundOrder(OutOrderDto dto) {
        List<ItemSkuDto> detailsList = dto.getDetailsList();
        if (ObjUtil.isNotEmpty(detailsList)){
            //定义总的数量
            int totalCount = 0;
            int totalWeight = 0;
            int totalVolume = 0;
            for (ItemSkuDto item : dto.getDetailsList()) {
                totalCount += item.getCount();
                ItemSku sku = skuMapper.selectById(item.getId());
                totalVolume = item.getCount()*sku.getProductVolume();
                totalWeight = item.getCount()*sku.getProductWeight();
            }
            // 生成8位数字随机数
            int eightDigitNumber = (int)(Math.random() * 90000000 + 10000000);
            String invoiceCode = "OUT"+eightDigitNumber;
            OutboundOrder outboundOrder = OutboundOrder.builder()
                    .invoiceCode(invoiceCode)
                    .totalCount(totalCount)
                    .totalVolume(totalVolume)
                    .totalWeight(totalWeight)
                    //预发货
                    .status(1)
                    .empId(dto.getCustomerId())
                    .customerId(dto.getCustomerId())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            getBaseMapper().insert(outboundOrder);

            //添加出库详情
            for (ItemSkuDto item : dto.getDetailsList()) {
                OutboundOrderDetails details = OutboundOrderDetails.builder()
                        .outboundOrderId(outboundOrder.getId())
                        .outCount(item.getCount())
                        .skuId(item.getId())
                        //未拣货
                        .pickStatus(1)
                        .createTime(LocalDateTime.now())
                        .updateTime(LocalDateTime.now())
                        .build();
                detailsMapper.insert(details);
            }
        }
    }

    //查新数据
    @Override
    public OutOrderDto queryById(Integer id) {
        OutboundOrder outboundOrder = getById(id);
        List<OutboundOrderDetails> list = detailsMapper.selectList(Wrappers.<OutboundOrderDetails>lambdaQuery()
                .eq(OutboundOrderDetails::getOutboundOrderId, outboundOrder.getId()));
        List<ItemSkuDto> itemSkuDtos = new ArrayList<>();
        list.forEach(item->{
            ItemSku sku = skuMapper.selectById(item.getSkuId());
            ItemSkuDto itemSkuDto = new ItemSkuDto();
            itemSkuDto.setSkuName(sku.getTitle());
            itemSkuDto.setId(item.getId());
            itemSkuDto.setCount(item.getOutCount());
            itemSkuDto.setSpuCode(sku.getCode());
            itemSkuDtos.add(itemSkuDto);
        });
        OutOrderDto dto = new OutOrderDto();

        dto.setId(outboundOrder.getId());
        dto.setCustomerId(outboundOrder.getCustomerId());
        dto.setDetailsList(itemSkuDtos);
        return dto;
    }

    @Override
    @Transactional
    public void updateByIdStatus(ConfirmOutOrderDto dto) {
        OutboundOrder outboundOrder = OutboundOrder.builder()
                //确定发货
                .status(2)
                .id(dto.getId())
                .build();
        updateById(outboundOrder);
        WarehouseAreaLocal local = localService.getById(dto.getWarehousingAreaLocalId());
        dto.getDetailsList().forEach(item->{
            OutboundOrderDetails details = OutboundOrderDetails.builder().updateTime(LocalDateTime.now())
                    .id(item.getId())
                    .targetWarehouseId(local.getWarehouseId())
                    .targetWarehouseAreaId(local.getWarehouseAreaId())
                    .targetWarehouseAreaLocaId(local.getId())
                    .build();
            detailsMapper.updateById(details);
        });
    }

    @Override
    public PageResult queryPageOut(Integer page, Integer pageSize, String invoiceCode, String customerName, String skuTitle) {
        PageHelper.startPage(page,pageSize);
        List<OutboundOutVo> list = getBaseMapper().queryPageOut(invoiceCode,customerName,skuTitle);
        Page<OutboundOutVo> outVoPage = (Page<OutboundOutVo>) list;
        return new PageResult(outVoPage.getTotal(),outVoPage.getResult());
    }

    //已经发货的状态
    @Override
    public void updateByIdOutbound(OutboundOutVo outbound) {
        OutboundOrder outboundOrder = OutboundOrder.builder().id(outbound.getId())
                .status(8)
                .updateTime(LocalDateTime.now())
                .build();
        getBaseMapper().updateById(outboundOrder);
    }

    @Override
    public PageResult queryPageReceive(Integer page, Integer pageSize, String invoiceCode, String customerName, String skuTitle) {
        PageHelper.startPage(page,pageSize);
        List<OutboundOutVo> list = getBaseMapper().queryPageReceive(invoiceCode,customerName,skuTitle);
        Page<OutboundOutVo> outVoPage = (Page<OutboundOutVo>) list;
        return new PageResult(outVoPage.getTotal(),outVoPage.getResult());
    }

    //已经签收的状态
    @Override
    public void updateByIdReceiveStatus(OutboundOutVo outbound) {
        OutboundOrder outboundOrder = OutboundOrder.builder().id(outbound.getId())
                .status(9)
                .updateTime(LocalDateTime.now())
                .build();
        getBaseMapper().updateById(outboundOrder);
    }

}
    